﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;

using Cisoft.Cisoft3G.BLL;
using Cisoft.Cisoft3G.Model;

public partial class Business_Cisoft3G_MemberManage3G_MemberManage3G : WebUI.Page.BasePage
{
    #region 页面加载
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            ltScript.Text = "<script type=\"text/javascript\" src=\"../../../js/Basic/BasicPage.js\"></script><script type=\"text/javascript\" src=\"../../../js/My97DatePicker/WdatePicker.js\"></script>";
            chcAll.Attributes.Add("onclick", "SelectAllCheckboxes(this)");
            Bind();
        }
    }
    #endregion

    #region 数据绑定
    /// <summary>
    /// 数据绑定
    /// </summary>
    private void Bind()
    {
        //当前页码
        int cpn = AspNetPager1.CurrentPageIndex;
        //获取数据
        DataTable dt = new Cisoft.Cisoft3G.BLL.ContactsBO().GetUserLst(cpn, 10, AppendString()).Tables[0];
        int pageNum = dt.Rows.Count;
        if (dt.Rows.Count > 0)
        {
            Repeater1.DataSource = dt;
            Repeater1.DataBind();
            AspNetPager1.RecordCount = new Cisoft.Cisoft3G.BLL.ContactsBO().GetUserLstCount(cpn, 10, AppendString());
            AspNetPager1.PageSize = 10;
        }
        else
        {
            Repeater1.DataSource = dt;
            Repeater1.DataBind();
            AspNetPager1.RecordCount = new Cisoft.Cisoft3G.BLL.ContactsBO().GetUserLstCount(cpn, 10, AppendString());
        }
    }
    #endregion

    #region 翻页事件
    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        Bind();
    }
    #endregion

    #region 查询事件
    /// <summary>
    /// 查询事件
    /// </summary>
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        Bind();
    }
    #endregion

    #region 删除
    /// <summary>
    /// 删除事件
    /// </summary>
    protected void Button1_Click(object sender, EventArgs e)
    {
        btnConfirm3.Visible = false;
        btnConfirm2.Visible = true;
        btnCancel.Visible = false;

        List<int> list = new List<int>();
        foreach (RepeaterItem item in Repeater1.Items)
        {
            CheckBox cbk_single = item.FindControl("cbk_single") as CheckBox;
            if (cbk_single.Checked)
            {
                Label lblContactID = item.FindControl("lblContactID") as Label;
                if (!string.IsNullOrEmpty(lblContactID.Text))
                {
                    list.Add(int.Parse(lblContactID.Text));
                }
            }
        }

        Cisoft.Cisoft3G.BLL.ContactsBO bll = new Cisoft.Cisoft3G.BLL.ContactsBO();
        string strError = string.Empty;
        if (list.Count > 0)
        {
            try
            {
                foreach (int id in list)
                {
                    //bll.Delete(id);
                }
            }
            catch (Exception ex)
            {
                strError = ex.Message;
            }
            if (string.IsNullOrEmpty(strError))
            {
                //PageRight("删除成功！", "ContactsManage.aspx", "返回 ", "");
                PageRight(ModalPopupExtender1, lblTS, btnCancel, "<h4>删除成功！</h4>");
                Bind();
            }
            else
            {
                PageError(ModalPopupExtender1, lblTS, btnCancel, "删除失败，原因如下：" + strError);
                //PageError("删除失败，原因如下：" + strError + "。", Request.RawUrl);
            }
        }
    }
    #endregion

    #region 获得查询条件
    private string AppendString()
    {
        StringBuilder sb = new StringBuilder();
        sb.Append(" 1=1 ");
        if (!string.IsNullOrEmpty(txtCondition.Value))
        {
            sb.Append(" and " + ddlSearchCondition.SelectedValue + " like '%" + txtCondition.Value.Trim() + "%' ");
        }
        sb.Append(" and CT_IsUser = 0 and US_UserID<>1");
        return sb.ToString();
    }

    #endregion

    #region 详细事件
    protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        if (e.CommandName == "Details")
        {
            Cisoft.Cisoft3G.BLL.ContactsBO bll = new Cisoft.Cisoft3G.BLL.ContactsBO();
            //Cisoft.Cisoft3G.Model.ContactsMO model = new Cisoft.Cisoft3G.Model.ContactsMO();

            DataSet ds = bll.GetUserAndContact(" CT_Mobile=" + e.CommandArgument.ToString());
            if (ds.Tables[0].Rows.Count > 0)
            {
                DataRow dr = ds.Tables[0].Rows[0];

                lblUserName.Text = dr["US_UserName"].ToString();
                lblUserRole.Text = new Cisoft.IntegrationSystem.BLL.UserRoleBO().GetModel(int.Parse(dr["US_Role_Rid"].ToString())).UserRoleName;
                lblAddress.Text = dr["CT_Address"].ToString();
                lblCompany.Text = dr["CT_Company"].ToString();
                lblCompanyAddress.Text = dr["CT_CompanyAddress"].ToString();
                lblContactGroup.Text = new Cisoft.Cisoft3G.BLL.ContactGroupBO().GetModel(int.Parse(dr["CG_ContactGroupID_FK"].ToString())).CG_GroupName;
                lblDepartment.Text = dr["CT_Department"].ToString();
                lblDuty.Text = dr["CT_Duty"].ToString();
                lblEmail.Text = dr["CT_Email"].ToString();
                lblMobile.Text = dr["CT_Mobile"].ToString();
                lblName.Text = dr["CT_Name"].ToString();
                lblPhone.Text = dr["CT_Phone"].ToString();
                lblPostCode.Text = dr["CT_PostCode"].ToString();
                lblQQ.Text = dr["CT_QQ"].ToString();

                LinkButton1_Click(source, e);
            }
        }
        if (e.CommandName == "Upper")
        {
            ViewState["UpperID"] = e.CommandArgument.ToString();
            lblUpperTS.Text = "<h5>您确认提升该会员为管理员吗？角色为默认管理员</h5>";

            btnConfirm3.Visible = true;
            btnConfirm2.Visible = false;
            btnCancel.Visible = true;

            LinkButton3_Click(source, e);
        }
    }
    #endregion

    #region 显示详细信息
    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        ModalPopupExtender3.Show();
    }

    protected void LinkButton3_Click(object sender, EventArgs e)
    {
        ModalPopupExtender2.Show();
    }
    #endregion

    #region 新增事件
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        Response.Redirect("ContactAdd.aspx");
    }
    #endregion

    #region 列表显示组名称
    protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        Cisoft.IntegrationSystem.BLL.UserRoleBO bll = new Cisoft.IntegrationSystem.BLL.UserRoleBO();

        Label lblrole = e.Item.FindControl("lblrole") as Label;
        try
        {
            lblrole.Text = bll.GetModel(int.Parse(lblrole.Text)).UserRoleName;
        }
        catch (Exception ex)
        {

        }
    }
    #endregion

    #region 自定义DataTable
    /// <summary>
    /// 自定义DataTable
    /// fxc 201003101209
    /// </summary>
    /// <returns>已经绑定了数据的DataTable</returns>
    private DataTable CreateDateTable()
    {
        DataTable dt = new DataTable();

        dt.Columns.Add(new DataColumn("姓名"));
        dt.Columns.Add(new DataColumn("性别"));
        dt.Columns.Add(new DataColumn("电话号码"));
        dt.Columns.Add(new DataColumn("手机号码"));
        dt.Columns.Add(new DataColumn("第二手机号码"));
        dt.Columns.Add(new DataColumn("电子邮箱"));
        dt.Columns.Add(new DataColumn("QQ号码"));
        dt.Columns.Add(new DataColumn("地址"));
        dt.Columns.Add(new DataColumn("邮政编码"));
        dt.Columns.Add(new DataColumn("单位"));
        dt.Columns.Add(new DataColumn("单位地址"));
        dt.Columns.Add(new DataColumn("部门"));
        dt.Columns.Add(new DataColumn("职务"));
        dt.Columns.Add(new DataColumn("通讯录组"));

        return dt;
    }
    #endregion

    #region 选择删除提示
    //显示选择删除
    protected void LinkButton2_Click(object sender, EventArgs e)
    {
        if (!string.IsNullOrEmpty(hihDelete.Value) && hihDelete.Value == "false")
        {
            lblTS.Text = "<br /><h4>请选择要删除的记录</h4><br />";
            btnConfirm2.Visible = true;
            btnCancel.Visible = false;
            ModalPopupExtender1.Show();
        }
        if (!string.IsNullOrEmpty(hihDelete.Value) && hihDelete.Value == "true")
        {
            lblTS.Text = "<br /><h4>你确认删除吗？</h4><br />";
            btnConfirm2.Visible = true;
            btnCancel.Visible = true;
            ModalPopupExtender1.Show();
        }
    }
    protected void btnConfirm2_Click(object sender, EventArgs e)
    {
        if (hihDelete.Value == "true")
        {
            Button1_Click(sender, e);
        }
        else
        {

        }
    }
    #endregion

    #region 查看详细并发送
    protected void btnSend_Click(object sender, EventArgs e)
    {
        Response.Redirect("../AddSms.aspx?Mobile=" + lblMobile.Text);
    }
    #endregion

    #region 提升会员为管理员
    protected void btnConfirm3_Click(object sender, EventArgs e)
    {
        string xmlPath = Server.MapPath(Request.ApplicationPath + "/xmlconfig/sys/cis.config");
        if (ViewState["UpperID"] != null && ViewState["UpperID"].ToString() != "")
        {
            int id = int.Parse(ViewState["UpperID"].ToString());

            ContactsBO bll = new ContactsBO();
            ContactsMO model = bll.GetModel(id);
            if (model != null)
            {
                model.CT_IsUser = 1;
            }

            Cisoft.IntegrationSystem.BLL.UserBO uBO = new Cisoft.IntegrationSystem.BLL.UserBO();
            
            try
            {
                 Cisoft.IntegrationSystem.Model.UserMO uModel = uBO.GetUserInfo(int.Parse(bll.GetUserAndContact(" CT_ContactID=" + id).Tables[0].Rows[0]["US_UserID"].ToString()));
                 if (uModel != null)
                 {
                     uModel.Userrole = int.Parse(Cisoft.IntegrationSystem.Common.XMLHelper.GetConfigValue("cisDefaultUserRole", xmlPath));
                 }

                bll.Update(model);

                uBO.EditRecord(uModel);

                btnCancel2.Visible = false;
                PageError(ModalPopupExtender2, lblUpperTS ,btnConfirm3, "成功提升该会员为管理员！");
            }
            catch (Exception ex)
            {
                PageError(ModalPopupExtender2, lblUpperTS, btnConfirm3, "操作出错！错误信息如下：" + ex.Message);
            }
        }
        Bind();
    }
    #endregion
}
